package ${packagename}.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ${packagename}.entity.${table.classname};
import ${packagename}.mapper.${table.classname}Mapper;
import ${packagename}.service.${table.classname}Service;
import org.springframework.transaction.annotation.Transactional;
import ${packagename}.vo.BootstrapTableVoJA;
import ${packagename}.vo.R;

@Service("${table.classname?uncap_first}Service")
public class ${table.classname}ServiceImpl implements ${table.classname}Service {

	@Autowired
	private ${table.classname}Mapper ${table.classname?uncap_first}Mapper;

	@Override
	@Transactional
	public R add(${table.classname} entity) throws Exception {
		
		// 添加数据
		if (${table.classname?uncap_first}Mapper.insert(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteFalse(Long ${table.pkjavaname}) throws Exception {
	
		if (${table.classname?uncap_first}Mapper.deleteFalse(${table.pkjavaname}) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteTrue(Long ${table.pkjavaname}) {
		
		${table.classname} entity = ${table.classname?uncap_first}Mapper.get(${table.pkjavaname});
		if (entity == null) {
			return R.error("该数据不存在");
		}
		
		${table.classname?uncap_first}Mapper.deleteTrue(${table.pkjavaname});
		
		return R.ok();
	}

	@Override
	@Transactional
	public R edit(${table.classname} entity) throws Exception {
		
		// 更新数据
		if (${table.classname?uncap_first}Mapper.update(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	public BootstrapTableVoJA search(${table.classname} cond) {
		
		List<Map<String, Object>> listMap = cond.queryMap();
		
		int total = cond.count();
		
        BootstrapTableVoJA vo = new BootstrapTableVoJA();
        vo.setRowsByListMap(listMap);
        vo.setTotal(total);
        
        return vo;
	}
}
